/**
 * Created by tangtao on 2017/05/10.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('AssessReportCtrl', AssessReportCtrl);

    function AssessReportCtrl(MemberModel, $scope,$ionicSlideBoxDelegate, $ionicPopup,AppUtils,$stateParams,LoginAccountAuth) {
        var vm = this;
        vm.AssessVo = {};
        vm.loginAccount = {};
        // vm.AssessVo.memberId = "8a80809d5aeb3be1015aeb3cb8540000";
        vm.AssessVo.memberId = $stateParams.memberId;
        // vm.AssessVo.startDateSize=null;
        // vm.AssessVo.endDateSize=null;
        var dateSize = 1;
        $ionicSlideBoxDelegate.update();
        var StringDate;
        var dateTypes;//作为时间滑动类型
        vm.fraction=null;
        
        vm.initDate = function() {
            LoginAccountAuth.getAccount().then(function(data){
                vm.loginAccount = data;
                vm.slideSize = [0,1,2];
                vm.memberType = data.memberType;
                if (vm.memberType == '2') {
                    MemberModel.findMemberId().$promise.then(function (response) {
                        vm.studentList = response.memberIds;
                        if (response.report == "null") {
                            vm.nullReports();
                        } else {
                            vm.fraction = response.report.fraction[0];
                            StringDate = response.report.date;
                            var attribute = response.report.attribute;
                            vm.AssessVo.startDateSize = response.report.startDate;
                            vm.AssessVo.endDateSize = response.report.endDate;
                            vm.highcharts('conns0',attribute);
                        }
                    });
                } else {
                    MemberModel.findAssessReport(vm.AssessVo).$promise.then(function (response) {
                        if (response.report == "null") {
                            vm.nullReports();
                        } else {
                            vm.fraction = response.report.fraction[0];
                            StringDate = response.report.date;
                            var attribute = response.report.attribute;
                            vm.AssessVo.startDateSize = response.report.startDate;
                            vm.AssessVo.endDateSize = response.report.endDate;
                            vm.highcharts('conns0',attribute);
                        }
                    });
                }
            })
        };
        $scope.onSwipeLeft = function (index) {
            if (vm.AssessVo.endDateSize == 7) {
                $ionicSlideBoxDelegate.slide(index);
                return;
            }
            if (vm.AssessVo.endDateSize == 0) {
                $ionicSlideBoxDelegate.slide(index);
                return;
            }
            if (dateTypes == "year") {
                $ionicSlideBoxDelegate.slide(index);
                return;
            }
            if (dateTypes == "month") {
                vm.AssessVo.endDateSize = vm.AssessVo.endDateSize +1;
                vm.AssessVo.startDateSize = vm.AssessVo.startDateSize + 1;
                vm.pageSizeLeft = index;
                MemberModel.findReportMonthYear(vm.AssessVo).$promise.then(function (response) {
                    if (response.report == "null") {
                        vm.nullReports();
                    } else {
                        vm.fraction = response.report.fraction[0];
                        dateTypes = response.report.dateTypes;
                        StringDate = response.report.date;
                        var attribute = response.report.attribute;
                        vm.AssessVo.startDateSize = response.report.startDate;
                        vm.AssessVo.endDateSize = response.report.endDate;
                        var index = vm.pageSizeLeft;
                        index += 1;
                        if (index == 3) {
                            index = 0;
                        }
                        var conns = 'conns';
                        conns+=index;
                        vm.highcharts(conns,attribute);
                        $androidActionSheet.fadeOut(200);
                    }
                });
            } else if (dateTypes == "year") {
                $ionicSlideBoxDelegate.slide(0);
                return;
            } else {
                vm.AssessVo.endDateSize = vm.AssessVo.endDateSize -7;
                vm.AssessVo.startDateSize = vm.AssessVo.startDateSize - 7;
                vm.pageSizeLeft = index;
                MemberModel.findReportSlide(vm.AssessVo).$promise.then(function (response) {
                    if (response.report == "null") {
                        vm.nullReports();

                    } else {
                        vm.fraction = response.report.fraction[0];
                        var attribute = response.report.attribute;
                        var index = vm.pageSizeLeft;
                        index += 1;
                        if (index == 3) {
                            index = 0;
                        }
                        var conns = 'conns';
                        conns+=index;
                        vm.highcharts(conns,attribute);
                    }
                });
            }
            console.log(index);
        };

        $scope.onSwipeRight = function (index) {
            // dateSize--;
            console.log(index);
            if (dateTypes == "month") {
                vm.pageSizeRight = index;
                vm.AssessVo.endDateSize = vm.AssessVo.endDateSize - 1;
                vm.AssessVo.startDateSize = vm.AssessVo.startDateSize - 1;
                vm.pageSizeLeft = index;
                MemberModel.findReportMonthYear(vm.AssessVo).$promise.then(function (response) {
                    if (response.report == "null") {
                        vm.nullReports();
                    } else {
                        vm.fraction = response.report.fraction[0];
                        dateTypes = response.report.dateTypes;
                        StringDate = response.report.date;
                        var attribute = response.report.attribute;
                        vm.AssessVo.startDateSize = response.report.startDate;
                        vm.AssessVo.endDateSize = response.report.endDate;
                        var index = vm.pageSizeLeft;
                        index += 1;
                        if (index == 3) {
                            index = 0;
                        }
                        var conns = 'conns';
                        conns+=index;
                        vm.highcharts(conns,attribute);
                        $androidActionSheet.fadeOut(200);
                    }
                });
            } else if (dateTypes == "year") {
                $ionicSlideBoxDelegate.slide(0);
                return;
            } else {
                vm.pageSizeRight = index;
                vm.AssessVo.endDateSize = vm.AssessVo.endDateSize + 7;
                vm.AssessVo.startDateSize = vm.AssessVo.startDateSize + 7;
                MemberModel.findReportSlide(vm.AssessVo).$promise.then(function (response) {
                    if (response.report == "null") {
                        vm.nullReports();
                    } else {
                        vm.fraction = response.report.fraction[0];
                        var attribute = response.report.attribute;
                        var index = vm.pageSizeRight;
                        if (index == 0) {
                            index = 2;
                        } else {
                            index -= 1;
                        }
                        var conns = 'conns';
                        conns+=index;
                        vm.highcharts(conns,attribute);
                    }
                });
            }
            console.log(index);
        };
        $scope.repeatDone = function() {
            $ionicSlideBoxDelegate.update();
            //$ionicSlideBoxDelegate.slide($scope.week.length - 1, 1);
        };

        var $androidActionSheet = $('#androidActionsheet');
        var $androidMask = $androidActionSheet.find('.weui-mask');

        $("#naviconReport").on('click', function(){
            $androidActionSheet.fadeIn(200);
            $androidMask.on('click',function () {
                $androidActionSheet.fadeOut(200);
                // alert(1);
            });
        });

        var $studentChoicesheet = $('#studentChoice');
        var $androidMasks = $studentChoicesheet.find('.weui-masks');
        $("#studentChoice").on('click', function () {
            $studentChoicesheet.fadeIn(200);
            $androidMasks.on('click',function () {
                $studentChoicesheet.fadeOut(200);
            });
        });
        $scope.choiceReport = function (elem) {
            var el = elem.target;
            var name = el.getAttribute("name");
            switch (name) {
                case 'week':
                    vm.initDate();
                    $androidActionSheet.fadeOut(200);
                    break;
                case 'month':
                    vm.AssessVo.dataType = 'month';
                    vm.AssessVo.startDateSize = 0;
                    vm.AssessVo.endDateSize = 0;
                    MemberModel.findReportMonthYear(vm.AssessVo).$promise.then(function (response) {
                        if (response.report == "null") {
                            vm.nullReports();
                        } else {
                            vm.fraction = response.report.fraction[0];
                            dateTypes = response.report.dateTypes;
                            StringDate = response.report.date;
                            var attribute = response.report.attribute;
                            vm.AssessVo.startDateSize = response.report.startDate;
                            vm.AssessVo.endDateSize = response.report.endDate;
                            vm.highcharts('conns0',attribute);
                            $ionicSlideBoxDelegate.slide(0);
                            $androidActionSheet.fadeOut(200);
                        }
                    });
                    break;
                case 'year':
                    vm.AssessVo.dataType = 'year';
                    MemberModel.findReportMonthYear(vm.AssessVo).$promise.then(function (response) {
                        if (response.report == "null") {
                            vm.nullReports();
                        } else {
                            vm.fraction = response.report.fraction[0];
                            StringDate = response.report.date;
                            var attribute = response.report.attribute;
                            vm.AssessVo.startDateSize = response.report.startDate;
                            vm.AssessVo.endDateSize = response.report.endDate;
                            vm.highcharts('conns0',attribute);
                            $ionicSlideBoxDelegate.slide(0);
                            $androidActionSheet.fadeOut(200);
                        }
                    });
                    break;
            }
        };
        vm.nullReports = function () {
            angular.element("#nullReport").css("display","block");
            angular.element(".reportUt").css("display","none");
            $androidActionSheet.fadeOut(200);
        };
        $scope.choiceStudent = function (elem) {
            var el = elem.target;
            var memberId = el.getAttribute("name");
            vm.AssessVo.memberId = memberId;
            MemberModel.findAssessReport(vm.AssessVo).$promise.then(function (response) {
                if (response.report == "null") {
                    vm.nullReports();
                } else {
                    vm.fraction = response.report.fraction[0];
                    StringDate = response.report.date;
                    var attribute = response.report.attribute;
                    vm.AssessVo.startDateSize = response.report.startDate;
                    vm.AssessVo.endDateSize = response.report.endDate;
                    vm.highcharts('conns0',attribute);
                }
            });
        };
        vm.initDate();


        vm.highcharts = function (name,data) {
            var chart = new Highcharts.Chart(name,{
                colors: ['#2b908f', '#90ee7e', '#f45b5b', '#7798BF', '#aaeeee', '#ff0066', '#eeaaee',
                    '#55BF3B', '#DF5353', '#7798BF', '#aaeeee'
                ],
                title: {
                    text: StringDate,
                    x: 0,
                    style: {
                        color: 'red'
                    }
                },
                chart: {
                    backgroundColor: {
                        linearGradient: {
                            x1: 0,
                            y1: 0,
                            x2: 1,
                            y2: 1
                        },
                        stops: [
                            [0, '#2a2a2b'],
                            [1, '#3e3e40']
                        ]
                    },
                    style: {
                        fontFamily: '\'Unica One\', sans-serif'
                    },
                    plotBorderColor: '#606063',
                    type: 'spline'
                },
                credits: {
                    // text: 'visionet.com',
                    // href: 'http://www.visionet.com.cn/',
                    enabled: false,
                    style: {
                        color: '#666'
                    }
                },

                xAxis: {
                    title: {
                        text:'以下属性均可点击',
                        style: {
                            color: '#A0A0A3'

                        }
                    },
                    categories:['周一','周二','周三','周四','周五'],
                    gridLineColor: '#707073',
                    labels: {
                        style: {
                            color: '#E0E0E3'
                        }
                    },
                    lineColor: '#707073',
                    minorGridLineColor: '#505053',
                    tickColor: '#707073',

                },
                yAxis: {
                    title: {
                        text:'',
                        style: {
                            color: '#A0A0A3'
                        }
                    },
                    plotLines:[{
                        value:2,
                        width:1,
                        color:''
                    }],
                    gridLineColor: '#2a2a2b',
                    gridLineWidth: 0,
                    labels: {
                        style: {
                            color: '#E0E0E3'
                        }
                    }
                },

                plotOptions: {
                    series: {
                        dataLabels: {
                            color: '#B0B0B3'
                        },
                        marker: {
                            lineColor: '#333'
                        }
                    },

                },
                tooltip:{
                    valueSuffix:'分',
                    backgroundColor: 'rgba(0, 0, 0, 0.85)',
                    style: {
                        color: '#F0F0F0'
                    }
                },
                legend:{
                    layout:'horizontal',
                    align:'center',
                    // verticalAlign: 'bottom',
                    borderWidth:0,
                    itemStyle: {
                        color: '#E0E0E3'
                    },
                    itemHoverStyle: {
                        color: '#FFF'
                    },
                    itemHiddenStyle: {
                        color: '#606063'
                    }
                },

                exporting:{
                    enabled:false
                },
                labels: {
                    style: {
                        color: '#707073'
                    }
                },
                series:[{
                    name:'情绪情况',
                    data:data.emotion,
                    visible: false
                },{
                    name:'合作交往',
                    data:data.communication,
                    visible: false
                },{
                    name:'动手操作',
                    data:data.operation,
                    visible: false
                },{
                    name:'学习习惯',
                    data:data.habit,
                    visible: false
                },{
                    name:'卫生习惯',
                    data:data.hygiene,
                    visible: false
                },{
                    name:'进餐情况',
                    data:data.theMeal,
                    visible: false
                },{
                    name:'生活自理',
                    data:data.sinceReason,
                    visible: false
                },{
                    name:'锻炼身体',
                    data:data.healthy,
                    visible: false
                },{
                    name:'睡眠情况',
                    data:data.sleep,
                    // data:["1","2","3"],不认字符串，需要从后台将字符串改为数字
                    events: {
                        click: function(e) {
                            alert(e.point.category);
                        }
                    }
                }]
            });
        }
    }
})();;